package com.example.administrator.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.bean.MsgContent;
import com.example.administrator.testapk.R;
import com.example.administrator.view.AtyNormalMsg;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/23/023.
 * 一般信息列表显示的适配器，添加点击和长按事件的回调接口
 */

public class MsgContentRecyclerViewAdapter extends RecyclerView.Adapter<MsgContentRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private List<MsgContent.DataBean.ListBean> mDatas;

    public MsgContentRecyclerViewAdapter(Context context, ArrayList<MsgContent.DataBean.ListBean> datas) {
        this.context = context;
        mDatas = datas;
    }

//    public void setData(List<String> data) {
//        mData = data;
//    }
//
    private MsgContentRecyclerViewAdapter.OnItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(MsgContentRecyclerViewAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener =  listener;
    }

    /**
     * 定义Item点击事件的回调接口
     */
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
//        void onLongItemClick(View view , int position);
    }

    @Override
    public MsgContentRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_normalmsg, parent, false);
        MsgContentRecyclerViewAdapter.ViewHolder vh = new MsgContentRecyclerViewAdapter.ViewHolder(view);
        //将创建的View注册点击事件
        return vh;
    }

    @Override
    public void onBindViewHolder(final MsgContentRecyclerViewAdapter.ViewHolder holder, int position) {
        holder.tv.setText(mDatas.get(position).getSystemName());
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);

        if(mOnItemClickListener != null){
            final int pos = holder.getLayoutPosition();
                //绑定点击事件
            holder.itemLinear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                }
            });

//           holder.itemLinear.setOnLongClickListener(new View.OnLongClickListener() {
//               @Override
//               public boolean onLongClick(View v) {
//                   mOnItemClickListener.onLongItemClick(holder.itemView, pos);
//                   return false;
//               }
//           });
        }
        }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0:mDatas.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView tv;
        private LinearLayout itemLinear;

        public ViewHolder(View itemView) {
            super(itemView);
            tv = (TextView)itemView.findViewById(R.id.tv_title_normalmsg);
            itemLinear = (LinearLayout)itemView.findViewById(R.id.itemLinear);
        }
    }
}
